package com.muyu.vehicle.process;

import com.muyu.vehicle.config.Config;
import com.muyu.vehicle.enums.VehicleStatusEnum;
import com.muyu.vehicle.service.IVehicleInfoService;
import com.muyu.vehicle.service.IVehicleMoveInfoService;
import com.vehicle.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author 牧鱼
 * @Classname ProcessVehicleOnLineThread
 * @Description TODO
 * @Date 2021/9/15
 */
public class ProcessVehicleOnLineThread implements Runnable{

    private static final Logger log = LoggerFactory.getLogger(ProcessVehicleOnLineThread.class);

    /**
     * redis
     */
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 车辆信息
     */
    private IVehicleInfoService vehicleInfoService;

    /**
     * 行驶记录
     */
    private IVehicleMoveInfoService vehicleMoveInfoService;

    public ProcessVehicleOnLineThread(RedisTemplate<String, String> redisTemplate,
                                      IVehicleInfoService vehicleInfoService,
                                      IVehicleMoveInfoService vehicleMoveInfoService) {
        this.redisTemplate = redisTemplate;
        this.vehicleInfoService = vehicleInfoService;
        this.vehicleMoveInfoService = vehicleMoveInfoService;
    }

    @Override
    public void run() {
        ListOperations<String, String> operations = redisTemplate.opsForList();
        String vin = operations.rightPop(Config.VEHICLE_START_LINE);
        if (StringUtils.isNotEmpty(vin)){
            log.info("车辆上线：VIN为[{}]",vin);
            vehicleInfoService.updateVehicleStatusByVin(vin, VehicleStatusEnum.ONLINE.getStatus());
            vehicleMoveInfoService.addVehicleMoveInfo(vin);
        }
    }
}
